package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WdyConsume;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.service.IWdyConsumeService;
import com.ruoyi.system.service.IWdyUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消费 信息操作处理
 * 
 * @author ruoyi
 * @date 2020-08-31
 */
@Controller
@RequestMapping("/system/wdyConsume")
public class WdyConsumeController extends BaseController
{
    private String prefix = "system/wdyConsume";
	@Autowired
	private IWdyUserService wdyUserService;
	@Autowired
	private IWdyConsumeService wdyConsumeService;
	
	@RequiresPermissions("system:wdyConsume:view")
	@GetMapping()
	public String wdyConsume()
	{
	    return prefix + "/wdyConsume";
	}
	
	/**
	 * 查询消费列表
	 */
	@RequiresPermissions("system:wdyConsume:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WdyConsume wdyConsume)
	{
		startPage();
        List<WdyConsume> list = wdyConsumeService.selectWdyConsumeList(wdyConsume);
		for(WdyConsume e : list) {
			QueryWrapper<WdyUser> query = new QueryWrapper<>();
			query.eq("user_id", e.getUserId());
			WdyUser user = wdyUserService.getOne(query);
			if(user != null) {
				e.addExtraData("mobile", user.getUserName());
			}
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出消费列表
	 */
	@RequiresPermissions("system:wdyConsume:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WdyConsume wdyConsume)
    {
    	List<WdyConsume> list = wdyConsumeService.selectWdyConsumeList(wdyConsume);
        ExcelUtil<WdyConsume> util = new ExcelUtil<WdyConsume>(WdyConsume.class);
        return util.exportExcel(list, "wdyConsume");
    }
	
	/**
	 * 新增消费
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存消费
	 */
	@RequiresPermissions("system:wdyConsume:add")
	@Log(title = "消费", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WdyConsume wdyConsume)
	{		
		return toAjax(wdyConsumeService.insertWdyConsume(wdyConsume));
	}

	/**
	 * 修改消费
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyConsume wdyConsume = wdyConsumeService.selectWdyConsumeById(id);
		mmap.put("wdyConsume", wdyConsume);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存消费
	 */
	@RequiresPermissions("system:wdyConsume:edit")
	@Log(title = "消费", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WdyConsume wdyConsume)
	{		
		return toAjax(wdyConsumeService.updateWdyConsume(wdyConsume));
	}
	
	/**
	 * 删除消费
	 */
	@RequiresPermissions("system:wdyConsume:remove")
	@Log(title = "消费", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wdyConsumeService.deleteWdyConsumeByIds(ids));
	}
	
}
